package com.eigaland.change_machine.business.movielist

import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.eigaland.change_machine.BR
import com.eigaland.change_machine.R
import com.eigaland.change_machine.adapter.BaseBindAdapter
import com.eigaland.change_machine.business.admin.AdminFragment
import com.eigaland.change_machine.business.movelistfullscreen.MovieListFullScreenFragment
import com.eigaland.change_machine.business.setup_account.SetUpAccountFragment
import com.eigaland.change_machine.business.show.ShowListFragment
import com.eigaland.change_machine.dialog.TakeTicketDialog
import com.eigaland.change_machine.util.ProgressDialogManager
import com.eigland.eigaland_lib.base.BaseLazyFragment
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.fragment_movie_list.*
import kotlinx.android.synthetic.main.fragment_movie_list.view.*
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator
import me.yokeyword.fragmentation.anim.FragmentAnimator
import kotlin.math.abs

public class MovieListFragment:BaseLazyFragment<MovieListFragmentVM,com.eigaland.change_machine.databinding.FragmentMovieListBinding>(){

    private val moveListAdapter by lazy { BaseBindAdapter<MovieListItemVM>(R.layout.item_movie_list_data, BR.movieListItemVM) }

    lateinit var dialogFragment : TakeTicketDialog



    private fun ticketDialogLogic(){
        vm?.dialogErrorMessage?.observe(this, Observer {
            dialogFragment.showErrorMsg(it)
        })

        vm?.takeTicketDialogSuccess?.observe(this, Observer {
            it.forEachIndexed { index, ticketRecordItem ->


                dialogFragment.showTakeStatus(index)
            }


            dialogFragment.showSuccessStatus()

        })

        vm?.showTakeTicketDialogStatus?.observe(this, Observer {
            dialogFragment = TakeTicketDialog()
            dialogFragment.rightClickLiveData.observe(this, Observer {
                if(it == "再次打印" ){
                    dialogFragment.resetStatus()
                } else {
                    vm?.takeTicket(it)
                    dialogFragment.showTakeStatus(0)

                }

            })

            dialogFragment.show(childFragmentManager,"")

        })
    }

    private fun movieDataLogic(){
        vm?.movieListDataVMS?.observe(this, Observer {
            moveListAdapter.run {
                this.addData(it.movieItems!!)
                View.VISIBLE
            }

            moveListAdapter.setOnItemClickListener { adapter, view, position ->

                var frg = ShowListFragment()
                var movieId = it.movieItems?.get(position)?.movieId
                var bundle = Bundle()
                bundle.putString("movieId",movieId)
                frg.arguments = bundle
                supportDelegate.start(frg)
            }

            rv_movie_list.layoutManager = LinearLayoutManager(requireContext(),RecyclerView.VERTICAL,false)
            rv_movie_list.adapter = moveListAdapter

            binding?.movielistIndicator?.setDataV(it.movieItems!!.size / 5,0)
            binding?.movielistIndicator?.setupRecycleView(rv_movie_list)

        })

        vm?.goToMovieListFullPageStatus?.observe(this, Observer {
            if(it) {
                supportDelegate.start(MovieListFullScreenFragment())
            }
        })

    }

    override fun initVM() {
        vm?.initData()
        vm?.progressLoadingStatus?.observe(this, Observer {
            if(it)
                ProgressDialogManager.showDialog(requireContext())
            else
                ProgressDialogManager.dismissDialog(requireContext())
        })

        movieDataLogic()
        ticketDialogLogic()


    }

    override fun setUpViewData(view: View) {
        vm = MovieListFragmentVM()
        binding = DataBindingUtil.bind(view)
        binding?.let {
            it.lifecycleOwner = this
            it.movieListVM = vm
        }

        var isGoToAdmin = false


        var gest = GestureDetector(requireContext(),object: GestureDetector.OnGestureListener{
            override fun onShowPress(e: MotionEvent?) {

            }

            override fun onSingleTapUp(e: MotionEvent?): Boolean {

                return false
            }

            override fun onDown(e: MotionEvent?): Boolean {


                return false


            }

            override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {


                return false


            }

            override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {

                when(e2?.action){
                    MotionEvent.ACTION_MOVE ->{
                        if(e2?.pointerCount > 0 && abs(distanceX) > 100f && !isGoToAdmin){
                            isGoToAdmin = true
                            // 三只手指
                            var fragment = SetUpAccountFragment()
                            fragment.arguments = Bundle().apply {
                                this.putBoolean("isAdmin",true)
                            }
                            supportDelegate.start(fragment)
                       }
                    }

                }
                return false

            }

            override fun onLongPress(e: MotionEvent?) {

            }

        })

        view.setOnTouchListener { v, event ->



            gest.onTouchEvent(event)
        }
    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        super.onFragmentResult(requestCode, resultCode, data)

    }


    override fun onCreateFragmentAnimator(): FragmentAnimator {
        return DefaultHorizontalAnimator()
    }

    override fun setLayoutId() = R.layout.fragment_movie_list

}